# # 导入模块
# import threading
# import time
import serial
import serial.tools.list_ports
#
# # 自定义变量
# port = "/dev/cu.Bluetooth-Incoming-Port"  # 端口号，根据自己实际情况输入，可以在设备管理器查看
# bps = 9600  # 串口波特率，根据自己实际情况输入
# timeout = 5  # 超时时间,None：永远等待操作，0为立即返回请求结果，其他值为等待超时时间(单位为秒）
# rxdata = ''  # 接收的数据
#
#
# 扫描端口

# port_list = list(serial.tools.list_ports.comports())
# # print(port_list)
# if len(port_list) == 0:
#     print('can not fine uart port')
#
# else:
#     for i in range(0, len(port_list)):
#         print(port_list[i])

#
#
# # 打开串口
# def open_uart(port, bps, timeout):
#     try:
#         # 打开串口，并返回串口对象
#         uart = serial.Serial(port, bps, timeout=timeout)
#         return uart
#     except Exception as result:
#         print("can not open uart")
#         print(result)
#         return False
#
#
# # 发送数据
# def uart_send_data(uart, txbuf):
#     len = uart.write(txbuf.encode('utf-8'))  # 写数据
#     return len
#
#
# # 接收数据
# def uart_receive_data(uart):
#     if uart.in_waiting:
#         rxdata = uart.read(uart.in_waiting).decode("utf-8")  # 以字符串接收
#         # rxdata = uart.read().hex()  # 以16进制(hex)接收
#         print(rxdata)  # 打印数据
#
#
# # 关闭串口
# def close_uart(uart):
#     uart.close()
#
#
# # 创建一个线程用来等待串口接收数据
# class myThread(threading.Thread):  # 继承父类threading.Thread
#     def __init__(self, uart):
#         threading.Thread.__init__(self)
#         self.uart = uart
#
#     def run(self):  # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
#         while True:
#             # print("thread_uart_receive")
#             uart_receive_data(self.uart)  # 接收数据
#             # time.sleep(0.01)
#
#
# # 主函数
# def main():
#     # 扫描端口
#     result = check_uart_port()
#     if (result == False):
#         return
#
#     # 打开串口
#     result = open_uart(port, bps, timeout)
#     if (result == False):
#         return
#     else:
#         uart1 = result
#
#     # 创建一个线程用来接收串口数据
#     thread_uart = myThread(uart1)
#     thread_uart.start()
#
#     while True:
#         # 定时发送数据
#         txbuf = "hello world"
#         len = uart_send_data(uart1, txbuf)
#         print("send len: ", len)
#         time.sleep(1)
#
#
# # 启动主函数
# main()

# import serial #导入模块
#
# try:
#     # 端口号，根据自己实际情况输入，可以在设备管理器查看
#     port = "/dev/cu.Bluetooth-Incoming-Port"
#     # 串口波特率，根据自己实际情况输入
#     bps = 9600
#     # 超时时间,None：永远等待操作，0为立即返回请求结果，其他值为等待超时时间(单位为秒）
#     time = 5
#     # 打开串口，并返回串口对象
#     uart = serial.Serial(port, bps, timeout = time)
#
#     # 串口发送一个字符串
#     len = uart.write("hello world".encode('utf-8'))
#     print("send len: ", len)
#
#     # 串口接收一个字符串
#     str = ''
#     for i in range(len):
#         str += uart.read().decode("utf-8")
#     print("receive data: ", str)
#
#     # 关闭串口
#     uart.close()
#
# except Exception as result:
#     print("******error******：", result)

#
# import re
# data = '$,0.450,0.000,0.345,\r\n'
#
# pai_order = data[:-2]
# kk = re.compile(r'[-0-9.0-9]+')
# pai = kk.findall(pai_order)
# if pai_order != '':
#     try:
#         thr = float(pai[0])
#         brake = float(pai[1])
#         steer = float(pai[2])
#         print(f"receive data:thr = {thr},brake = {brake}, steer = {steer}")
#     except:
#             thr = 0.0
#             brake = 0.0
#             steer = 0.0
#             print("Error obtaining Raspberry PI information")
# import re
#
# x = 0.0
# y = 0.0
# ho_out = 0.0
# v = 0.0
# model = 2
# thr = 0.0
# brake = 0.0
# steer = 0.0
# soc = 99
# data = str(round(x, 3)) + ','
# data = data + str(round(y, 3)) + ','
# data = data + str(round(ho_out, 3)) + ','
# data = data + str(round(v, 3)) + ','
# data = data + str(int(model)) + ','
# data = data + str(round(thr, 3)) + ','
# data = data + str(round(brake, 3)) + ','
# data = data + str(steer) + ','
# data = data + str(int(soc)) + '\r\n'
# kk = re.compile(r'\d+\.?\d+|\d+|-\d+\.?\d+')
# info = data
# xyv = kk.findall(info)
# print(xyv)
import keyboard



# while True:
#     if keyboard.is_pressed('w'):
#         print('Forward')
#     elif keyboard.is_pressed('s'):
#         print('Backward')
#     elif keyboard.is_pressed('a'):
#         print('Left')
#     elif keyboard.is_pressed('d'):
#         print('Right')
#     elif keyboard.is_pressed('enter'):  # if key 'enter' is pressed
#         print('You pressed enter!')
#     elif keyboard.is_pressed('q'):
#         print('Quit!')
#         break

import pygame
pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("窗口")
background = pygame.image.load("Background.jpg").convert()
while True:
    pygame.event.get()
        # keys_pressed = pygame.key.get_pressed()
        # if keys_pressed == pygame.KEYDOWN:
        #     if keys_pressed == pygame.K_a:
        #         print("left")
        #     elif keys_pressed == pygame.K_d:
        #         print("right")
        #     elif keys_pressed == pygame.K_w:
        #         print("front")
        #     elif keys_pressed == pygame.K_s:
        #         print("back")
        #     else:
        #         print("None")
    print("first")
    keys_pressed = pygame.key.get_pressed()
    if keys_pressed[pygame.K_3]:
        print("model 3")
    if keys_pressed[pygame.K_w]:
        print("front")

    if keys_pressed[pygame.K_a]:
        print("left")
    if keys_pressed[pygame.K_s]:
        print("back")
    if keys_pressed[pygame.K_d]:
        print("right")
    if keys_pressed[pygame.K_SPACE]:
        print("kongge")
    # else:
    #     print("None")
    screen.blit(background, (0, 0))
    pygame.display.update()
